package models.statics
{
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	[Bindable]
	public class CurrencyUtil
	{
		public static var currencyCodes:ArrayCollection = new ArrayCollection();

		public static function requestCurrencyCodes():void{
			if( currencyCodes.length <= 0 ){
				var service:HTTPService = new HTTPService();
				service.useProxy = false;
				service.url = "assets/xmldata/CurrencyCodeList.xml";
				service.resultFormat = "e4x";
				
				service.addEventListener(ResultEvent.RESULT, handleSuccess);
				service.addEventListener(FaultEvent.FAULT, handleFault);
				service.send();
			}
		}
		private static function handleSuccess(evt:ResultEvent):void{
			var xml:XML = evt.result as XML;
			for each ( var Code:XML in xml..Currency ){
				currencyCodes.addItem(
					{code: Code.CurrencyCoded, 
					name: Code.CurrencyName}
				);
			};
//			Alert.show( "I've catched the currency code list!!!", 'Hurray' );
		}
		private static function handleFault(evt:FaultEvent):void{
			Alert.show( evt.fault.message, 'Some error occured' );
		}
	}
}